<?php
include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/Snoopy.class.php');
include_once('../lib/function.php');
include_once('/usr/local/xunsearch/sdk/php/lib/XS.php');

//将未入数据库的搜索数据插入数据库
$redis = new Redis();  
$ret = $redis->connect("localhost","6379");  //php客户端设置的ip及端口
if(!$ret) die('redis connction faild');
$redis->auth(REDIS_PASS);
$redis->select(2);

$GLOBALS['config']['master']['db_database'] = 'gouyg';
$GLOBALS['config']['slave']['db_database']  = 'gouyg';

$basedir = dirname(__FILE__);
$model = new Db;

$p = 1;
while (true) {
    try {
        $xs = new XS('booksonline');
        $index  = $xs->index;
        $search = $xs->search;
        $search->setCharset('UTF-8');
        $search->setQuery('*');

        $p = max(1, intval($p));
        $n = XSSearch::PAGE_SIZE;
        $search->setLimit($n, ($p - 1) * $n);
        $docs = $search->search();

        $count = $search->getLastCount();
        $total = $search->getDbTotal();
        echo2('count:'.$count.', total:'.$total.', '.count($docs).", {$p}/{$n}");

        if($count == 0 || count($docs) == 0) break;

        foreach ($docs as $doc) {
            $book_data = [
                'md5id' => $doc->md5id,
                'url' => $doc->url,
                'title' => $doc->title,
                'description' => $doc->description,
                'last_chapter_url' => $doc->last_chapter_url,
                'last_chapter' => $doc->last_chapter,
                'author' => $doc->author,
                'size' => $doc->size,
                'updatetime' => $doc->updatetime,
                'state' => $doc->state,
                'indextime' => $doc->indextime,
            ];

            // 存储book信息
            $old_book = $model->find('book', "md5id='".$doc->md5id."'");
            if(!$old_book) {
                $r = $model->add('book', $book_data);
                if(!$r) {
                    $error = $model->error('book insert error');
                    throw new Exception($error['info']);
                } else {
                    $book_id = $model->insert_id();
                }
            } else {
                $book_id = $old_book['id'];
            }

            echo2('book_id:'.$book_id);

            //7天内有更新则放入连载队列
            if($book_data['updatetime'] >= time() - 3600*24*7) {
                unset($book_data['description']);
                $redis->hSet('book_ongoing', $book_data['md5id'], json_encode($book_data));
            }
            continue;

            // 存储chapter信息
            $chapters_list = $redis->get('books:'.$doc->md5id);
            if($chapters_list) {
                $chapters_list = json_decode($chapters_list, true);

                $model->startTrans();
                foreach ($chapters_list['urls'] as $k => $url) {
                    $localfile = '/read_all/'.$doc->md5id.'/'.md5($url);
                    $cachefile = $basedir . $localfile;
                    if(file_exists($cachefile)) {
                        $save_localfile = $localfile;
                    } else {
                        $save_localfile = '';
                    }

                    $old_chapter = $model->find('book_chapter', "md5url='".md5($url)."'");
                    if(!$old_chapter) {
                        $chapter_data = [
                            'book_id'=>$book_id,
                            'md5url'=>md5($url),
                            'url'=>$url,
                            'title'=>$chapters_list['titles'][$k],
                            'local_file'=>$save_localfile,
                            'add_time'=>time(),
                        ];
                        $r = $model->add('book_chapter', $chapter_data);
                        if(!$r) {
                            $error = $model->error('chapter insert error');
                            throw new Exception($error['info']);
                        } else {
                            $chapter_id = $model->insert_id();
                        }
                    } else {
                        $chapter_id = $old_chapter['id'];
                        if($save_localfile && empty($old_chapter['local_file'])) {
                            $model->save('book_chapter', "id='{$chapter_id}'", ['local_file'=>$save_localfile]);
                            echo2('update local_file:'.$save_localfile);
                        }
                    }

                    echo2('book_id:'.$book_id.', chapter_id:'.$chapter_id);
                }

                $model->commit();
            }
        }
        $p++;

    } catch (Exception $e) {
        var_dump($e->getMessage());

        $model->rollback();
        $model->end();

        sleep(5);
    }
}
